package com.itfreer.data.metadata;

import java.io.InputStream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import com.itfreer.data.config.ConfigVersionService;
import com.itfreer.data.metadata.dao.DataDefinitionDao;
import com.itfreer.data.metadata.entity.DataDefinitionEntity;
import com.itfreer.form.api.BaseDao;
import com.itfreer.form.api.DataInputResult;
import com.itfreer.form.api.DictionaryServiceImp;
import com.itfreer.form.api.dictionary.DictionaryEntity;

/**
 * 定义数据定义实现类
 */
@Component
public class DataDefinitionServiceImp extends DictionaryServiceImp<DataDefinitionEntity>
		implements DataDefinitionService {

	@Autowired(required = false)
	private DataDefinitionDao dao;
	@Autowired(required = false)
	private ConfigVersionService configVersionService;

	@Override
	protected BaseDao<DataDefinitionEntity> getDao() {
		return dao;
	}

	@Override
	public String getDictionaryName() {
		return "g_data_definition";
	}

	/**
	 * 是否为租户间共享实体对象
	 */
	protected boolean isShareTenant() {
		return true;
	}

	/**
	 * 获取租户间共享字段名称 用于SAAS下数据管理
	 */
	protected String getShareTenantFieldName() {
		return "shareTenantID";
	}

	@Override
	protected DictionaryEntity convert(DataDefinitionEntity entity) {
		DictionaryEntity toEntity = new DictionaryEntity();
		toEntity.setId(entity.getId());
		toEntity.setCode(entity.getId());
		toEntity.setDisplay(entity.getDataname());
		toEntity.setValid(entity.getValid());
		toEntity.setOrder(entity.getOrder());
		toEntity.setTenantID(entity.getTenantID());
		toEntity.setTenantName(entity.getTenantName());
		toEntity.setAbbreviation(entity.getDatatable());
		toEntity.setInfo(entity.getObjectIdFieldName());
		toEntity.setShareTenantID(entity.getShareTenantID());
		toEntity.setUnits(entity.getDatatype());
		return toEntity;
	}

	@Override
	protected String getDictionaryFields() {
		return "*";
	}

	/**
	 * 添加实体
	 * 
	 * @param entity 添加实体
	 * @return
	 */
	@Override
	public DataDefinitionEntity add(DataDefinitionEntity entity) {
		configVersionService.addVersion();
		return super.add(entity);
	}

	/**
	 * 更新实体
	 * 
	 * @param guid   唯一值
	 * @param entity 实体
	 * @return
	 */
	@Override
	public DataDefinitionEntity update(DataDefinitionEntity entity) {
		configVersionService.addVersion();
		return super.update(entity);
	}

	/**
	 * 指量删除实体
	 * 
	 * @param guid 唯一值集
	 */
	@Override
	public Boolean deletes(String guids) {
		configVersionService.addVersion();
		return super.deletes(guids);
	}

	/**
	 * 删除实体
	 * 
	 * @param guid 唯一值
	 */
	@Override
	public Boolean delete(String guid) {
		configVersionService.addVersion();
		return super.delete(guid);
	}

	/**
	 * 逻辑删除恢复
	 * 
	 * @param guid
	 */
	@Override
	public Boolean reply(String guid) {
		configVersionService.addVersion();
		return super.reply(guid);
	}

	/**
	 * 导入Excel数据
	 * 
	 * @param fileNmae   文件名
	 * @param fileStream 数据文件
	 * @param inputID    导入配置
	 * @param startrow   开始导入行
	 * @return
	 */
	@Override
	public DataInputResult input(String fileName, InputStream fileStream, String inputID, Integer startrow) {
		configVersionService.addVersion();
		return super.input(fileName, fileStream, inputID, startrow);
	}

	/**
	 * 导入Shape数据
	 * 
	 * @param fileNmae 文件名
	 * @return
	 */
	@Override
	public DataInputResult importShape(String fileName, String inputID) {
		configVersionService.addVersion();
		return super.importShape(fileName, inputID);
	}

	/**
	 * 共享
	 * 
	 * @param id
	 * @param shareTenantID
	 * @param shareTenantName
	 * @return
	 */
	public Boolean share(String id, String shareTenantID, String shareTenantName) {
		DataDefinitionEntity entity = getEntity(id);
		if (entity == null) {
			return true;
		}
		// 配置事务策略
		DefaultTransactionDefinition def = new DefaultTransactionDefinition();
		def.setName("planOne-transaction");
		def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
		// 设置状态点
		TransactionStatus status = transactionManager.getTransaction(def);
		Boolean result = false;
		try {

			if (entity.getShareTenantID() == null || entity.getShareTenantID().equals("")) {
				entity.setShareTenantID("," + shareTenantID + ",");
			} else {
				if (entity.getShareTenantID().indexOf("," + shareTenantID + ",") == -1) {
					entity.setShareTenantID(entity.getShareTenantID() + shareTenantID + ",");
				}
			}
			if (entity.getShareTenantName() == null || entity.getShareTenantName().equals("")) {
				entity.setShareTenantName("," + shareTenantName + ",");
			} else {
				if (entity.getShareTenantName().indexOf("," + shareTenantName + ",") == -1) {
					entity.setShareTenantName(entity.getShareTenantName() + shareTenantName + ",");
				}
			}
			update(entity);
		} catch (Exception e) {
			transactionManager.rollback(status);
			throw e;
		}
		transactionManager.commit(status);
		return result;
	}
}
